package com.xuan.juc.volatiletest;

import java.util.concurrent.TimeUnit;

/*
* 可见性
* */
public class VTest1 {

    /*
    * 不加 volatile 程序会死循环
    *
    * */
    private volatile static int num = 0;

    public void add(){
        num++;
    }

    public static void main(String[] args) {
        new Thread(()->{  // 线程不知道主内存的变化
            while (num == 0){

            }
        }).start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        num = 1;

        System.out.println(num);
    }
}
